package com.stu.stusystem.service.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.stusystem.common.ApiException;
import com.stu.stusystem.mapper.system.StuMapper;
import com.stu.stusystem.model.system.StuUser;
import com.stu.stusystem.service.system.dto.UserManagerDTO;
import com.stu.stusystem.service.system.dto.UserManagerQuery;
import com.stu.stusystem.service.system.vo.StuUserVO;
import com.stu.stusystem.util.CopyBeanUtil;
import com.stu.stusystem.util.StrUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author cxt
 * @date 2020/9/29
 * 用户管理：根据权限查询用户列表
 */
@Service
public class UserManagerService {

    private StuMapper stuMapper;

    private DocumentService documentService;

    /**
     * 分页查询
     */
    public PageInfo<StuUserVO> getStuMangerPage(UserManagerQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<StuUserVO> stuMangerPage = this.stuMapper.getStuMangerPage(query);
        for (StuUserVO vo : stuMangerPage) {
            String s = StrUtil.hideSix(vo.getIdCard());
            vo.setIdCard(s);
        }
        return new PageInfo<>(stuMangerPage);
    }

    /**
     * 更新用户信息
     */
    public void updateStuManager(UserManagerDTO dto) {
        StuUser stuUser = this.stuMapper.selectByPrimaryKey(dto.getId());
        if (stuUser == null) {
            throw new ApiException(ApiException.BAD_REQUEST);
        }
        // 判断头像是否修改
        if (dto.getHeadImg() != null && stuUser.getHeadImg() != null && !"".equals(stuUser.getHeadImg())) {
            if (!stuUser.getHeadImg().equals(dto.getHeadImg())) {
                this.documentService.remove(stuUser.getHeadImg());
            }
        }
        CopyBeanUtil.copyIgnore(dto, stuUser);
        StuUser user = (StuUser) SecurityUtils.getSubject().getPrincipal();
        stuUser.setUpdateBy(user.getName());
        stuUser.setUpdateTime(new Date());
        // 身份证号如果是加密的就不要改了
        boolean b = StrUtil.judgeIdCard(stuUser.getIdCard());
        if (b) {
            stuUser.setIdCard(null);
        }
        int i = this.stuMapper.updateByExampleSelective(stuUser, new Example.Builder(StuUser.class)
                .where(WeekendSqls.<StuUser>custom().andEqualTo(StuUser::getId, dto.getId())).build());
        if (i != 1) {
            throw new ApiException(ApiException.UPDATE_FAIL);
        }
    }

    /**
     * 删除用户
     */
    public void deleteStu(String id) {
        if (this.stuMapper.deleteByPrimaryKey(id) != 1) {
            throw new ApiException(ApiException.DELETE_FAIL);
        }
    }

    /**
     * 重置密码
     */
    public String resetPwd(String id) {
        StuUser stuUser = this.stuMapper.selectByPrimaryKey(id);
        if (stuUser == null) {
            throw new ApiException("找不到重置的账号，请刷新页面重试");
        }
        StuUser user = (StuUser) SecurityUtils.getSubject().getPrincipal();
        String s = new Sha1Hash("1", stuUser.getAccount(), 5).toBase64();
        stuUser.setPassword(s);
        stuUser.setUpdateTime(new Date());
        stuUser.setUpdateBy(user.getName());
        int i = this.stuMapper.updateByPrimaryKeySelective(stuUser);
        if (i != 1) {
            throw new ApiException("系统错误，请刷新重试");
        }
        return "重置成功!新密码：1";
    }

    /**
     * 获取全部用户列表
     */
    public List<Map<String, String>> getWhole() {
        return this.stuMapper.getWhole();
    }

    @Autowired
    public void setStuMapper(StuMapper stuMapper) {
        this.stuMapper = stuMapper;
    }

    @Autowired
    public void setDocumentService(DocumentService documentService) {
        this.documentService = documentService;
    }
}
